package be.lioche.soupe;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Effect;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Egg;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.inventory.InventoryType.SlotType;
import org.bukkit.event.painting.PaintingBreakByEntityEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerEggThrowEvent;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.server.ServerListPingEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.Vector;

@SuppressWarnings("deprecation")
public class Event implements Listener{

	public Main plugin;
	public Event(Main main) {
		this.plugin = main;
	}

	/*-----------------------------------------GESTION DÉCO/RECO-----------------------------------------------*/

	@EventHandler
	public void connection(PlayerJoinEvent e){
		Player p = e.getPlayer();
		final Location spawn = new Location(p.getWorld(), p.getWorld().getSpawnLocation().getX()+0.5, p.getWorld().getSpawnLocation().getY()+1, p.getWorld().getSpawnLocation().getZ()+0.5);

		/* ------------------------------------- SCOREBOARDS------------------------------------------ */

		plugin.vieScore(p);
		plugin.persoScoreboard(p);
		List<Entity> list = p.getWorld().getEntities();



		for(int i = 0; i < list.size(); i++){
			Entity en = list.get(i);

			if(en.getType() == EntityType.DROPPED_ITEM || en.getType() == EntityType.PRIMED_TNT){
				en.remove();
			}
		}

		/* ------------------------------------- PLAYER ACTIONS------------------------------------------ */
		p.teleport(spawn);
		p.getInventory().clear();
		p.getInventory().setHelmet(null);
		p.getInventory().setChestplate(null);
		p.getInventory().setLeggings(null);
		p.getInventory().setBoots(null);
		p.playSound(p.getLocation(), Sound.CAT_MEOW, 1, 1);
		p.setMaxHealth(20.0);
		p.setHealth(20.0);
		p.setSaturation(50000);
		p.setGameMode(GameMode.ADVENTURE);
		p.setWalkSpeed((float) 0.2);
		p.removePotionEffect(PotionEffectType.SPEED);
		p.removePotionEffect(PotionEffectType.ABSORPTION);
		p.setLevel(0);


		ItemStack hat = new ItemStack(Material.FENCE_GATE);
		ItemMeta hatmeta = hat.getItemMeta();
		hatmeta.setDisplayName("§eOreilles sexy's :3");
		hat.setItemMeta(hatmeta);

		p.getInventory().setHelmet(hat);

		ItemStack shop = new ItemStack(Material.EMERALD);
		ItemMeta shopmeta = shop.getItemMeta();
		shopmeta.setDisplayName("§f[§cBoutique§f]");

		final List <String> shoplore = new ArrayList<String>();
		shoplore.add("");
		shoplore.add("§fAméliorations des Kits");
		shoplore.add("");

		shopmeta.setLore(shoplore);
		shop.setItemMeta(shopmeta);

		p.getInventory().addItem(shop);		

		/* ------------------------------------- SET ALLOWKIT------------------------------------------ */
		plugin.getConfig().set(p.getName()+".CanKit", Boolean.valueOf(true));
		System.out.println("Section .CanKit définie à true pour " + p.getName());

		/* ------------------------------------- SET KILLS STREAKS------------------------------------------ */
		plugin.getConfig().set(p.getName()+".Kills", Integer.valueOf(0));
		System.out.println("Section .Kills définie à 0 pour " + p.getName());

		/* ------------------------------------- SET TOTALS KILLS------------------------------------------ */
		if(plugin.getConfig().getInt(p.getName()+".TotalKills") >= 0){
			System.out.println("Section .TotalKills reprise à "+plugin.getConfig().getInt(p.getName()+".TotalKills")+" pour " + p.getName());
		}else{
			plugin.getConfig().set(p.getName()+".TotalKills", Integer.valueOf(0));
			System.out.println("Section .TotalKills définie à 0 pour " + p.getName());
		}

		/* ------------------------------------- SET TOTALS MORTS------------------------------------------ */
		if(plugin.getConfig().getInt(p.getName()+".Morts") >= 0){
			System.out.println("Section .Morts reprise à "+plugin.getConfig().getInt(p.getName()+".Morts")+" pour " + p.getName());
		}else{
			plugin.getConfig().set(p.getName()+".Morts", Integer.valueOf(0));
			System.out.println("Section .Morts définie à 0 pour " + p.getName());
		}

		/* ------------------------------------- SET FLASH KIT------------------------------------------ */
		if(plugin.getConfig().getBoolean(p.getName()+".Kits.flash.load") != true){
			plugin.getConfig().set(p.getName()+".Kits.flash.lvl", Integer.valueOf(1));
			plugin.getConfig().set(p.getName()+".Kits.flash.buy", Boolean.valueOf(false));
			plugin.getConfig().set(p.getName()+".Kits.flash.load", Boolean.valueOf(true));

			System.out.println("Section .Kits.flash définie à false pour " + p.getName()+ " et lvl: " + plugin.getConfig().getInt(p.getName()+".Kits.flash.lvl") + " et flash.load mis à: " + plugin.getConfig().getBoolean(p.getName()+".Kits.flash.load"));
		}else{
			System.out.println("Section .Kits.flash repris pour " + p.getName());
		}

		/* ------------------------------------- SET FREEZER KIT------------------------------------------ */
		if(plugin.getConfig().getBoolean(p.getName()+".Kits.freezer.load") != true){
			plugin.getConfig().set(p.getName()+".Kits.freezer.lvl", Integer.valueOf(1));
			plugin.getConfig().set(p.getName()+".Kits.freezer.buy", Boolean.valueOf(false));
			plugin.getConfig().set(p.getName()+".Kits.freezer.load", Boolean.valueOf(true));

			System.out.println("Section .Kits.freezer définie à false pour " + p.getName()+ " et lvl: " + plugin.getConfig().getInt(p.getName()+".Kits.freezer.lvl") + " et freezer.load mis à: " + plugin.getConfig().getBoolean(p.getName()+".Kits.freezer.load"));
		}else{
			System.out.println("Section .Kits.freezer repris pour " + p.getName());
		}

		/* ------------------------------------- SET TERRORISTE KIT------------------------------------------ */
		if(plugin.getConfig().getBoolean(p.getName()+".Kits.terroriste.load") != true){
			plugin.getConfig().set(p.getName()+".Kits.terroriste.lvl", Integer.valueOf(1));
			plugin.getConfig().set(p.getName()+".Kits.terroriste.buy", Boolean.valueOf(false));
			plugin.getConfig().set(p.getName()+".Kits.terroriste.load", Boolean.valueOf(true));

			System.out.println("Section .Kits.terroriste définie à false pour " + p.getName()+ " et lvl: " + plugin.getConfig().getInt(p.getName()+".Kits.terroriste.lvl") + " et terroriste.load mis à: " + plugin.getConfig().getBoolean(p.getName()+".Kits.terroriste.load"));
		}else{
			System.out.println("Section .Kits.terroriste repris pour " + p.getName());
		}

		/* ------------------------------------- SET DEFAUT KIT------------------------------------------ */
		if(plugin.getConfig().getBoolean(p.getName()+".Kits.defaut.load") != true){
			plugin.getConfig().set(p.getName()+".Kits.defaut.lvl", Integer.valueOf(1));
			plugin.getConfig().set(p.getName()+".Kits.defaut.load", Boolean.valueOf(true));

			System.out.println("Section .Kits.defaut définie à false pour " + p.getName()+ " et lvl: " + plugin.getConfig().getInt(p.getName()+".Kits.flash.lvl") + " et flash.load mis à: " + plugin.getConfig().getBoolean(p.getName()+".Kits.flash.load"));
		}else{
			System.out.println("Section .Kits.defaut repris pour " + p.getName());
		}

		/* ------------------------------------- KIT PAR DEFAUT------------------------------------------ */
		if(plugin.getConfig().getString(p.getName()+".havekit") == null){
			plugin.getConfig().set(p.getName()+".havekit", "defaut");

			System.out.println("Section .havekit défini à defaut pour " + p.getName());
		}else{
			System.out.println("Section .havekit repris à "+ plugin.getConfig().getString(p.getName()+".havekit") +" pour " + p.getName());
		}

		/* COINS */
		if(plugin.getConfig().getInt(p.getName()+".Coins") > 0){
			System.out.println("Section .Coins reprise à "+plugin.getConfig().getInt(p.getName()+".Coins")+" pour " + p.getName());
		}else{
			plugin.getConfig().set(p.getName()+".Coins", Integer.valueOf(25000)); // BETA TEST
			System.out.println("Section .TotalKills définie à 25000 pour " + p.getName());
		}

		/* ------------------------------------- SAVE CONFIG -------------------------------------------- */
		plugin.saveConfig();

		/* --------------------------------- JOIN MESSAGE + VERSION----------------------------------- */
		e.setJoinMessage(Main.name + p.getName() + " a rejoint le serveur !");
		p.sendMessage("§e§oPvP-Soupe | " + plugin.getConfig().getString("version")+" !"); 
		p.sendMessage("");
		p.sendMessage("§e§oLe serveur étant en « beta », 25 000 coins vous sont offert.");
		p.sendMessage("§e§oVous pouvez essayer les kits à volonté.");
		p.sendMessage("");
		//p.sendMessage("§cRESET DES COINS EFFECTUÉ LE 06 AVRIL À 0:36");
		//p.sendMessage("§cPLUS AUCUN COINS NE SERA GIVÉ.");
	}

	@EventHandler
	public void deconnection(PlayerQuitEvent e){
		Player p = e.getPlayer();
		e.setQuitMessage(Main.name + p.getName() + " a quitté le serveur !");	

	}

	@EventHandler
	public void MOTD(ServerListPingEvent e){
		if(!Bukkit.getServer().hasWhitelist()){
			e.setMotd(plugin.getConfig().getString("motd") + " (" + plugin.getConfig().getString("version") + ")");
		}else{
			e.setMotd("§cServeur en maitenance !" + " (" + plugin.getConfig().getString("version") + ")");
		}
	}

	@EventHandler
	public void whitelist(PlayerLoginEvent e){
		if(e.getKickMessage().equalsIgnoreCase("You are not white-listed on this server!")){
			e.setKickMessage(Main.name+"Serveur en maintenance !");
		}
	}

	@EventHandler
	public void kick(PlayerKickEvent e){
		if(e.getReason().contains("Kicked")){
			e.setReason(Main.name+"Kické par un administrateur !");
		}
	}

	/*----------------------------------------------REGEN SOUPES-----------------------------------------------*/
	@EventHandler
	public void joueurCliqueSoupe(PlayerInteractEvent e){
		Player p = e.getPlayer();
		Double vie = p.getHealth();

		if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
			if(p.getItemInHand().getType() == Material.MUSHROOM_SOUP){
				if(p.getItemInHand().getAmount() == 1){
					if(p.getHealth() <= 12){
						p.getInventory().getItemInHand().setType(Material.BOWL);
						p.setHealth(vie+8.0);
					}else if(p.getMaxHealth() == p.getHealth()){
						// NOTHING
					}else{
						p.getInventory().getItemInHand().setType(Material.BOWL);
						p.setHealth(20.0);
					}
				}else if(p.getItemInHand().getAmount() > 1){
					p.sendMessage(Main.name + "- §oÉtale tes soupes dans ton inventaire !");
				}
			}
		}
	}

	/*------------------------------------------BONUS + TP SPAWN-------------------------------------------*/
	@EventHandler
	public void nodrop(EntityDeathEvent e){
		Player p = (Player) e.getEntity();

		if(p.getName() != e.getEntity().getKiller().getName()){

			e.getDrops().clear();

			final Location spawn = new Location(p.getWorld(), 13.5, 83, -15.5);
			p.teleport(spawn);

			p.removePotionEffect(PotionEffectType.SPEED);
			p.removePotionEffect(PotionEffectType.ABSORPTION);

			plugin.getConfig().set(p.getName()+".CanKit", Boolean.valueOf(true));
			plugin.saveConfig();

			((PlayerDeathEvent) e).setDeathMessage("");
			p.setMaxHealth(20.0);
		}else{
			e.getDrops().clear();
		}

	}

	/*-----------------------------------------REMOVE DEGATS CHUTES---------------------------------------------*/
	@EventHandler
	public void nofall(EntityDamageEvent e){


		DamageCause c = e.getCause();

		if(e.getEntity() instanceof Player){
			Player p = (Player) e.getEntity();

			if(c == DamageCause.FALL || c == DamageCause.SUFFOCATION || c == DamageCause.DROWNING || c == DamageCause.STARVATION){
				e.setCancelled(true);
			}
			if(p.getLocation().getY() > 95.0){
				e.setCancelled(true);
			}

			if(c == DamageCause.ENTITY_EXPLOSION || c == DamageCause.BLOCK_EXPLOSION){
				e.setDamage(4);
			}

		}
	}

	/*----------------------------------------GESTION DES ENDERPEARLS-------------------------------------------*/
	@EventHandler
	public void enderRide(ProjectileLaunchEvent e){
		Entity en = e.getEntity();
		final Player p = (Player)e.getEntity().getShooter();
		final Vector v = en.getVelocity();

		if(en.getType() == EntityType.ENDER_PEARL){
			en.remove();

			Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

				public void run() {
					p.setVelocity(v.multiply(1.5));

				}
			}, 1L);



		}

	}

	/*-----------------------------------------REMOVE BOLS DROPPÉS----------------------------------------------*/
	@EventHandler
	public void removeBol(PlayerDropItemEvent e){
		Item i = e.getItemDrop();

		if(i.getItemStack().getType() == Material.BOWL){
			i.remove();
		}

	}

	/*-------------------------------------------EMPECHE CASSE BLOCS--------------------------------------------*/
	@EventHandler
	public void casseBlocs(BlockBreakEvent e){
		Player p = e.getPlayer();

		if(p.getGameMode() != GameMode.CREATIVE){
			e.setCancelled(true);
		}
	}
	@EventHandler
	public void poseblocs(BlockPlaceEvent e){
		Player p = e.getPlayer();

		if(p.getGameMode() != GameMode.CREATIVE){
			e.setCancelled(true);
		}
	}
	@EventHandler
	public void bleremove(BlockPhysicsEvent e){

		e.setCancelled(true);
		//e.getBlock().setType(Material.SOIL);
	}

	/*-----------------------------------------COINS QUAND KILLS + BONUS----------------------------------------*/
	@EventHandler
	public void bonusKills(PlayerDeathEvent e){
		e.setDeathMessage("");
		ItemStack ender = new ItemStack(Material.ENDER_PEARL, 2);

		Player p = (Player)e.getEntity();
		Player k = p.getKiller();
		p.setWalkSpeed((float) 0.2);
		e.getDrops().clear();
		e.setDroppedExp(0);

		/* RESET KILL STREAK DU TUÉ  ET MORT*/
		plugin.getConfig().set(p.getName()+".Morts", plugin.getConfig().getInt(p.getName()+".Morts")+1);
		plugin.getConfig().set(p.getName()+".Kills", Integer.valueOf(0));
		/* SI TUEUR EST PAS LE TUÉ */
		if(p.getName() != k.getName()){

			/* GIVE COINS ET +1 KILL STREAK POUR TUEUR */
			plugin.getConfig().set(k.getName()+".TotalKills", Integer.valueOf(plugin.getConfig().getInt(k.getName()+".TotalKills")+1));
			plugin.getConfig().set(k.getName()+".Kills", Integer.valueOf(plugin.getConfig().getInt(k.getName()+".Kills")+1));

			k.setLevel(plugin.getConfig().getInt(k.getName()+".Kills"));
			k.getInventory().addItem(ender);

			plugin.saveConfig();
			k.sendMessage("§c§o- " + p.getName() +" éliminé. (§f" + plugin.getConfig().getInt(k.getName()+".Kills") +"§c)");
			p.sendMessage("§c§o- Éliminé par " + k.getName());
			
			if(!k.isDead()){
				if(plugin.getConfig().getString(k.getName()+".havekit").equalsIgnoreCase("freezer")){
					ItemStack fleches = new ItemStack(Material.ARROW, 5);
					ItemMeta flechesmeta = fleches.getItemMeta();
					flechesmeta.setDisplayName("§f[§bFlèches gelées§f]");
					fleches.setItemMeta(flechesmeta);

					k.getInventory().addItem(fleches);
				}
			}
			if(!k.isDead()){
				if(plugin.getConfig().getString(k.getName()+".havekit").equalsIgnoreCase("terroriste")){
					ItemStack oeuf = new ItemStack(Material.EGG, 2);
					ItemMeta oeufmeta = oeuf.getItemMeta();
					oeufmeta.setDisplayName("§f[§cGrenada§f]");
					oeuf.setItemMeta(oeufmeta);

					k.getInventory().addItem(oeuf);
				}
			}

			/* SI 5 KILLS */
			if(plugin.getConfig().getInt(k.getName()+".Kills") == 5){

				Bukkit.broadcastMessage(Main.name + k.getName() + " a enchainé 5 kills !");
				k.sendMessage("");
				plugin.getConfig().set(k.getName()+".Coins", Integer.valueOf(plugin.getConfig().getInt(k.getName()+".Coins")+15));
				k.sendMessage(Main.name + "+15 coins (§cbonus 5 kills§f) ! [§c" + plugin.getConfig().getInt(k.getName()+".Coins") +"§f]");
				k.removePotionEffect(PotionEffectType.ABSORPTION);
				k.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 600, 1));
				k.damage(4.0);
				plugin.saveConfig();

				/* SI 10 KILLS */
			}else if(plugin.getConfig().getInt(k.getName()+".Kills") == 10){

				Bukkit.broadcastMessage(Main.name + "Incroyable ! " + k.getName() + " a atteint 10 kills !");
				k.sendMessage("");
				plugin.getConfig().set(k.getName()+".Coins", Integer.valueOf(plugin.getConfig().getInt(k.getName()+".Coins")+25));
				k.sendMessage(Main.name + "+25 coins (§cbonus 10 kills§f) ! [§c" + plugin.getConfig().getInt(k.getName()+".Coins") +"§f]");
				k.removePotionEffect(PotionEffectType.ABSORPTION);
				k.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 600, 1));
				k.damage(4.0);
				plugin.saveConfig();

				/* SI 20 KILLS */	
			}else if(plugin.getConfig().getInt(k.getName()+".Kills") == 15){

				Bukkit.broadcastMessage(Main.name + "Magique ! " + k.getName() + " viens de faire 15 kills !");
				plugin.getConfig().set(k.getName()+".Coins", Integer.valueOf(plugin.getConfig().getInt(k.getName()+".Coins")+35));
				k.sendMessage("");
				k.sendMessage(Main.name + "+30 coins (§cbonus 15 kills§f) ! [§c" + plugin.getConfig().getInt(k.getName()+".Coins") +"§f]");
				k.removePotionEffect(PotionEffectType.ABSORPTION);
				k.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 600, 1));
				k.damage(4.0);
				plugin.saveConfig();

			}else if(plugin.getConfig().getInt(k.getName()+".Kills") == 20){

				Bukkit.broadcastMessage(Main.name + "Hallucinant ! " + k.getName() + " viens de faire 20 kills !");
				plugin.getConfig().set(k.getName()+".Coins", Integer.valueOf(plugin.getConfig().getInt(k.getName()+".Coins")+35));
				k.sendMessage("");
				k.sendMessage(Main.name + "+35 coins (§cbonus 20 kills§f) ! [§c" + plugin.getConfig().getInt(k.getName()+".Coins") +"§f]");
				k.removePotionEffect(PotionEffectType.ABSORPTION);
				k.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 600, 1));
				k.damage(4.0);
				plugin.saveConfig();

			}else{ // SI PAS 5-10-20 KILLS


				plugin.getConfig().set(k.getName()+".Coins", Integer.valueOf(plugin.getConfig().getInt(k.getName()+".Coins")+10));
				k.sendMessage(Main.name + "+10 coins ! [§c" + plugin.getConfig().getInt(k.getName()+".Coins") +"§f]");

				k.removePotionEffect(PotionEffectType.ABSORPTION);
				k.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 600, 1));
				k.damage(4.0);

				e.setDeathMessage("");
			}


		}

		e.setDeathMessage("");
		p.setMaxHealth(20.0);
	}

	/* ----------------------------------------------RESPAWN---------------------------------------------------*/
	@EventHandler
	public void respawn(PlayerRespawnEvent e){
		Player p = e.getPlayer();
		final Location spawn = new Location(p.getWorld(), p.getWorld().getSpawnLocation().getX(), p.getWorld().getSpawnLocation().getY()+1, p.getWorld().getSpawnLocation().getZ());


		ItemStack shop = new ItemStack(Material.EMERALD);
		ItemMeta shopmeta = shop.getItemMeta();
		shopmeta.setDisplayName("§f[§cBoutique§f]");

		final List <String> shoplore = new ArrayList<String>();
		shoplore.add("");
		shoplore.add("§fAméliorations des Kits");
		shoplore.add("");

		shopmeta.setLore(shoplore);
		shop.setItemMeta(shopmeta);

		p.getInventory().addItem(shop);

		ItemStack hat = new ItemStack(Material.FENCE_GATE);
		ItemMeta hatmeta = hat.getItemMeta();
		hatmeta.setDisplayName("§eOreilles sexy's :3");
		hat.setItemMeta(hatmeta);

		p.getInventory().setHelmet(hat);

		e.getPlayer().teleport(spawn);
		plugin.getConfig().set(e.getPlayer().getName()+".CanKit", Boolean.valueOf(true));
		plugin.getConfig().set(e.getPlayer().getName()+".Kills", Integer.valueOf(0));
		plugin.saveConfig();

		/*  SCOREBOARD REFRESH */

		plugin.persoScoreboard(p);

	}

	/*------------------------------------------DROP ARMOR/ARMES-----------------------------------------------*/
	@EventHandler
	public void onArmorSlot(InventoryClickEvent event) {
		if (event.getSlotType().equals(SlotType.ARMOR)){
			if(event.getWhoClicked().getGameMode() != GameMode.CREATIVE){
				event.setCancelled(true);
			}
		}
	}

	@EventHandler
	public void dropArmor(PlayerDropItemEvent e){
		if(e.getItemDrop().getItemStack().getType() == Material.IRON_HELMET || e.getItemDrop().getItemStack().getType() == Material.IRON_CHESTPLATE ||e.getItemDrop().getItemStack().getType() == Material.IRON_LEGGINGS || e.getItemDrop().getItemStack().getType() == Material.IRON_BOOTS || e.getItemDrop().getItemStack().getType() == Material.IRON_SWORD || e.getItemDrop().getItemStack().getType() == Material.MUSHROOM_SOUP || e.getItemDrop().getItemStack().getType() == Material.REDSTONE_TORCH_ON || e.getItemDrop().getItemStack().getType() == Material.LEVER || e.getItemDrop().getItemStack().getType() == Material.ENDER_PEARL || e.getItemDrop().getItemStack().getType() == Material.EGG|| e.getItemDrop().getItemStack().getType() == Material.DIAMOND_SWORD|| e.getItemDrop().getItemStack().getType() == Material.STONE_SWORD || e.getItemDrop().getItemStack().getType() == Material.EGG|| e.getItemDrop().getItemStack().getType() == Material.EMERALD){
			e.setCancelled(true);
			e.getItemDrop().remove();
		}
	}

	/*------------------------------------------TORCHE KIT FLASH-----------------------------------------------*/
	@EventHandler
	public void activetorch(PlayerInteractEvent e){

		final Player p = e.getPlayer();
		if(plugin.getConfig().getBoolean(p.getName()+".CanKit") != true){
			if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
				if(e.getPlayer().getItemInHand().getType() == Material.REDSTONE_TORCH_ON){

					Block targetblock = e.getPlayer().getTargetBlock(null, 30);
					Location eye = targetblock.getLocation();
					eye.setY(eye.getY()+1);
					//Vector v = eye.getDirection();

					p.teleport(eye);
					p.playSound(p.getLocation(), Sound.ENDERMAN_TELEPORT, 1, 1);


					p.getInventory().remove(Material.REDSTONE_TORCH_ON);

					ItemStack torchoff = new ItemStack(Material.LEVER);
					ItemMeta torchoffmeta = torchoff.getItemMeta();
					torchoffmeta.setDisplayName(ChatColor.RED + "Téléportation - Rechargement");

					List <String> loretorchoff = new ArrayList<String>();
					loretorchoff.add("");
					loretorchoff.add("§fPatientez 15 secondes !");
					loretorchoff.add("");

					torchoffmeta.setLore(loretorchoff);
					torchoff.setItemMeta(torchoffmeta);

					p.getInventory().addItem(torchoff);

					Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable() {

						@Override
						public void run() {
							if(plugin.getConfig().getBoolean(p.getName()+".CanKit") != true && p.getInventory().contains(Material.LEVER)){
								ItemStack torch = new ItemStack(Material.REDSTONE_TORCH_ON);
								ItemMeta torchmeta = torch.getItemMeta();
								torchmeta.setDisplayName(ChatColor.RED + "Téléportation");

								List <String> loretorch = new ArrayList<String>();
								loretorch.add("");
								loretorch.add("§fLe rechargement dure 15s !");
								loretorch.add("");

								torchmeta.setLore(loretorch);
								torch.setItemMeta(torchmeta);

								p.getInventory().remove(Material.LEVER);
								p.getInventory().addItem(torch);
							}
						}
					}, 300L);

				}
			}
		}
	}

	/*-----------------------------------------------SHOP-----------------------------------------------------*/
	@EventHandler
	public void shop(PlayerInteractEvent e){
		Player p = e.getPlayer();

		if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
			if(p.getItemInHand().getType() == Material.EMERALD){
				Shop.openshop(p);
			}
		}
	}
	@EventHandler
	public void shopclick(InventoryClickEvent e){
		Player p = (Player) e.getWhoClicked();

		if(e.getInventory().getName().equalsIgnoreCase("[§4Boutique§r] "+plugin.getConfig().getInt(p.getName()+".Coins")+" coins")){
			e.setCancelled(true);

			if(e.getCurrentItem() == null || e.getCurrentItem().getType() == Material.AIR || !e.getCurrentItem().hasItemMeta()){
				p.closeInventory();
				return;
			}

			switch(e.getCurrentItem().getType()){
			case IRON_SWORD:
				if(plugin.getConfig().getInt(p.getName()+".Kits.defaut.lvl") == 1){
					if(plugin.getConfig().getInt(p.getName()+".Coins") >= 100){
						plugin.getConfig().set(p.getName()+".Kits.defaut.lvl", Integer.valueOf(2));
						plugin.getConfig().set(p.getName()+".Kits.defaut.load", Boolean.valueOf(true));
						plugin.getConfig().set(p.getName()+".Coins", Integer.valueOf(plugin.getConfig().getInt(p.getName()+".Coins")-100));
						plugin.saveConfig();
						p.closeInventory();
						Shop.openshop(p);

						plugin.persoScoreboard(p);
					}else{
						p.sendMessage(Main.name + "Vous n'avez pas assez de coins ! [§c"+plugin.getConfig().getInt(p.getName()+".Coins")+"§f]");
					}
				}else if(plugin.getConfig().getInt(p.getName()+".Kits.defaut.lvl") == 2){
					if(plugin.getConfig().getInt(p.getName()+".Coins") >= 150){
						plugin.getConfig().set(p.getName()+".Kits.defaut.lvl", Integer.valueOf(3));
						plugin.getConfig().set(p.getName()+".Kits.defaut.load", Boolean.valueOf(true));
						plugin.getConfig().set(p.getName()+".Coins", Integer.valueOf(plugin.getConfig().getInt(p.getName()+".Coins")-150));

						plugin.saveConfig();
						p.closeInventory();
						Shop.openshop(p);

						plugin.persoScoreboard(p);
					}else{
						p.sendMessage(Main.name + "Vous n'avez pas assez de coins ! [§c"+plugin.getConfig().getInt(p.getName()+".Coins")+"§f]");
					}
				}else{
					p.sendMessage(Main.name + "Vous avez déjà le kit Flash au niv.max !");
				}
				break;
			case REDSTONE_TORCH_ON:
				if(plugin.getConfig().getBoolean(p.getName()+".Kits.flash.buy") == true){
					if(plugin.getConfig().getInt(p.getName()+".Kits.flash.lvl") == 1){
						if(plugin.getConfig().getInt(p.getName()+".Coins") >= 150){
							plugin.getConfig().set(p.getName()+".Kits.flash.lvl", Integer.valueOf(2));
							plugin.getConfig().set(p.getName()+".Kits.flash.load", Boolean.valueOf(true));
							plugin.getConfig().set(p.getName()+".Coins", Integer.valueOf(plugin.getConfig().getInt(p.getName()+".Coins")-150));

							plugin.saveConfig();
							p.closeInventory();
							Shop.openshop(p);

							plugin.persoScoreboard(p);
						}else{
							p.sendMessage(Main.name + "Vous n'avez pas assez de coins ! [§c"+plugin.getConfig().getInt(p.getName()+".Coins")+"§f]");
						}
					}else if(plugin.getConfig().getInt(p.getName()+".Kits.flash.lvl") == 2){
						if(plugin.getConfig().getInt(p.getName()+".Coins") >= 200){
							plugin.getConfig().set(p.getName()+".Kits.flash.lvl", Integer.valueOf(3));
							plugin.getConfig().set(p.getName()+".Kits.flash.load", Boolean.valueOf(true));
							plugin.getConfig().set(p.getName()+".Coins", Integer.valueOf(plugin.getConfig().getInt(p.getName()+".Coins")-200));

							plugin.saveConfig();
							p.closeInventory();
							Shop.openshop(p);

							plugin.persoScoreboard(p);
						}else{
							p.sendMessage(Main.name + "Vous n'avez pas assez de coins ! [§c"+plugin.getConfig().getInt(p.getName()+".Coins")+"§f]");
						}
					}else{
						p.sendMessage(Main.name + "Vous avez déjà le kit Flash au niv.max !");
					}
				}
				break;
			case SNOW_BALL:
				if(plugin.getConfig().getBoolean(p.getName()+".Kits.freezer.buy") == true){
					if(plugin.getConfig().getInt(p.getName()+".Kits.freezer.lvl") == 1){
						if(plugin.getConfig().getInt(p.getName()+".Coins") >= 200){
							plugin.getConfig().set(p.getName()+".Kits.freezer.lvl", Integer.valueOf(2));
							plugin.getConfig().set(p.getName()+".Kits.freezer.load", Boolean.valueOf(true));
							plugin.getConfig().set(p.getName()+".Coins", Integer.valueOf(plugin.getConfig().getInt(p.getName()+".Coins")-200));

							plugin.saveConfig();
							p.closeInventory();
							Shop.openshop(p);

							plugin.persoScoreboard(p);
						}else{
							p.sendMessage(Main.name + "Vous n'avez pas assez de coins ! [§c"+plugin.getConfig().getInt(p.getName()+".Coins")+"§f]");
						}
					}else if(plugin.getConfig().getInt(p.getName()+".Kits.freezer.lvl") == 2){
						if(plugin.getConfig().getInt(p.getName()+".Coins") >= 250){
							plugin.getConfig().set(p.getName()+".Kits.freezer.lvl", Integer.valueOf(3));
							plugin.getConfig().set(p.getName()+".Kits.freezer.load", Boolean.valueOf(true));
							plugin.getConfig().set(p.getName()+".Coins", Integer.valueOf(plugin.getConfig().getInt(p.getName()+".Coins")-250));

							plugin.saveConfig();
							p.closeInventory();
							Shop.openshop(p);

							plugin.persoScoreboard(p);
						}else{
							p.sendMessage(Main.name + "Vous n'avez pas assez de coins ! [§c"+plugin.getConfig().getInt(p.getName()+".Coins")+"§f]");
						}
					}else{
						p.sendMessage(Main.name + "Vous avez déjà le kit Freezer au niv.max !");
					}
				}
				break;
			case TNT:
				if(plugin.getConfig().getBoolean(p.getName()+".Kits.terroriste.buy") == true){
					if(plugin.getConfig().getInt(p.getName()+".Kits.terroriste.lvl") == 1){
						if(plugin.getConfig().getInt(p.getName()+".Coins") >= 200){
							plugin.getConfig().set(p.getName()+".Kits.terroriste.lvl", Integer.valueOf(2));
							plugin.getConfig().set(p.getName()+".Kits.terroriste.load", Boolean.valueOf(true));
							plugin.getConfig().set(p.getName()+".Coins", Integer.valueOf(plugin.getConfig().getInt(p.getName()+".Coins")-200));

							plugin.saveConfig();
							p.closeInventory();
							Shop.openshop(p);

							plugin.persoScoreboard(p);
						}else{
							p.sendMessage(Main.name + "Vous n'avez pas assez de coins ! [§c"+plugin.getConfig().getInt(p.getName()+".Coins")+"§f]");
						}
					}else if(plugin.getConfig().getInt(p.getName()+".Kits.terroriste.lvl") == 2){
						if(plugin.getConfig().getInt(p.getName()+".Coins") >= 250){
							plugin.getConfig().set(p.getName()+".Kits.terroriste.lvl", Integer.valueOf(3));
							plugin.getConfig().set(p.getName()+".Kits.terroriste.load", Boolean.valueOf(true));
							plugin.getConfig().set(p.getName()+".Coins", Integer.valueOf(plugin.getConfig().getInt(p.getName()+".Coins")-250));

							plugin.saveConfig();
							p.closeInventory();
							Shop.openshop(p);

							plugin.persoScoreboard(p);
						}else{
							p.sendMessage(Main.name + "Vous n'avez pas assez de coins ! [§c"+plugin.getConfig().getInt(p.getName()+".Coins")+"§f]");
						}
					}else{
						p.sendMessage(Main.name + "Vous avez déjà le kit Terroriste au niv.max !");
					}
				}
				break;
			default:
				break;
			}
		}
	}

	/*------------------------------------------FREEZER ARC--------------------------------------------*/
	@EventHandler
	public void flecheglace(EntityDamageByEntityEvent e){		
		ItemStack glace = new ItemStack(Material.ICE);
		ItemMeta glacemeta = glace.getItemMeta();
		glacemeta.setDisplayName("§f[§bFreezer'hat§f]");
		glace.setItemMeta(glacemeta);

		final ItemStack casque = new ItemStack(Material.IRON_HELMET);
		final ItemStack casqueterro = new ItemStack(Material.CHAINMAIL_HELMET);
		final ItemStack casqueterro1 = new ItemStack(Material.CHAINMAIL_HELMET);
		casqueterro1.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
		final ItemStack casqueterro2 = new ItemStack(Material.CHAINMAIL_HELMET);
		casqueterro2.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
		final ItemStack casquedefaut = new ItemStack(Material.IRON_HELMET);
		casquedefaut.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
		final ItemStack casqueflash = new ItemStack(Material.PUMPKIN);
		casqueflash.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 6);





		if(e.getDamager() instanceof Arrow){
			Arrow arrow = (Arrow) e.getDamager();
			if(e.getEntity() instanceof Player){
				final Player t = (Player)e.getEntity();
				final Player p = (Player) arrow.getShooter();

				if(p != t){			
					t.setWalkSpeed((float) 0.0);
					t.sendMessage(Main.name+"Vous êtes ralenti par "+p.getName());
					p.sendMessage(Main.name+"Vous avez ralenti "+t.getName());
					t.getInventory().setHelmet(glace);
					Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable() {

						@Override
						public void run() {
							if(!t.isDead()){
								if(plugin.getConfig().getString(t.getName()+".havekit").equalsIgnoreCase("defaut")){
									if(plugin.getConfig().getInt(t.getName()+".Kits.defaut.lvl") == 1){
										t.setWalkSpeed((float) 0.2);
										t.getInventory().setHelmet(casque);
									}else if(plugin.getConfig().getInt(t.getName()+".Kits.defaut.lvl") == 2){
										t.setWalkSpeed((float) 0.25);
										t.getInventory().setHelmet(casque);
									}else if(plugin.getConfig().getInt(t.getName()+".Kits.defaut.lvl") == 3){
										t.setWalkSpeed((float) 0.25);
										t.getInventory().setHelmet(casquedefaut);
									}
								}

								if(plugin.getConfig().getString(t.getName()+".havekit").equalsIgnoreCase("flash")){
									if(plugin.getConfig().getInt(t.getName()+".Kits.flash.lvl") == 1){
										t.setWalkSpeed((float) 0.25);
										t.getInventory().setHelmet(casqueflash);
									}else if(plugin.getConfig().getInt(t.getName()+".Kits.flash.lvl") == 2){
										t.setWalkSpeed((float) 0.3);
										t.getInventory().setHelmet(casqueflash);
									}else if(plugin.getConfig().getInt(t.getName()+".Kits.flash.lvl") == 3){
										t.setWalkSpeed((float) 0.35);
										t.getInventory().setHelmet(casqueflash);
									}
								}

								if(plugin.getConfig().getString(t.getName()+".havekit").equalsIgnoreCase("terroriste")){
									if(plugin.getConfig().getInt(t.getName()+".Kits.terroriste.lvl") == 1){
										t.setWalkSpeed((float) 0.2);
										t.getInventory().setHelmet(casqueterro);
									}else if(plugin.getConfig().getInt(t.getName()+".Kits.terroriste.lvl") == 2){
										t.setWalkSpeed((float) 0.25);
										t.getInventory().setHelmet(casqueterro1);
									}else if(plugin.getConfig().getInt(t.getName()+".Kits.terroriste.lvl") == 3){
										t.setWalkSpeed((float) 0.3);
										t.getInventory().setHelmet(casqueterro2);
									}
								}

								if(plugin.getConfig().getString(t.getName()+".havekit").equalsIgnoreCase("freezer")){
									if(plugin.getConfig().getInt(t.getName()+".Kits.freezer.lvl") == 1){
										t.setWalkSpeed((float) 0.15);
										t.getInventory().setHelmet(casque);
									}else if(plugin.getConfig().getInt(t.getName()+".Kits.freezer.lvl") == 2){
										t.setWalkSpeed((float) 0.20);
										t.getInventory().setHelmet(casque);
									}else if(plugin.getConfig().getInt(t.getName()+".Kits.freezer.lvl") == 3){
										t.setWalkSpeed((float) 0.20);
										t.getInventory().setHelmet(casque);
									}
								}
							}
						}
					}, 60L);
				}
			}
		}
	}

	/*--------------------------------------------ANTI GRIEF TNT----------------------------------------------*/
	@EventHandler
	public void onExplosion(EntityExplodeEvent e){
		e.blockList().clear();
	}

	/*--------------------------------------------DROP / INVENTORY / PAINTING----------------------------------------------*/
	@EventHandler
	public void norecup(PlayerPickupItemEvent e){
		if(e.getPlayer().getGameMode() != GameMode.CREATIVE){
			e.setCancelled(true);
		}
	}
	@EventHandler
	public void onInventoryOpen(InventoryOpenEvent event) {
		if (event.getInventory().getType() == InventoryType.ANVIL || event.getInventory().getType() == InventoryType.CRAFTING || event.getInventory().getType() == InventoryType.ENCHANTING || event.getInventory().getType() == InventoryType.FURNACE || event.getInventory().getType() == InventoryType.ENDER_CHEST || event.getInventory().getType() == InventoryType.WORKBENCH || event.getInventory().getType() == InventoryType.BEACON ||event.getInventory().getType() == InventoryType.BREWING ) {
			event.setCancelled(true);
		}else if(event.getInventory().getType() == InventoryType.CHEST){
			if(event.getInventory().getName().contains("chest") || event.getInventory().getName() == null){
				event.setCancelled(true);
			}else{
			}
		}
	}
	@EventHandler
	public void protecttableaux(PaintingBreakByEntityEvent e){
		Player p = (Player) e.getRemover();
		if(p.getGameMode() != GameMode.CREATIVE){
			e.setCancelled(true);
		}
	}

	@EventHandler
	public void armorstand(PlayerInteractAtEntityEvent e){
		Entity en = e.getRightClicked();
		if(e.getPlayer().getGameMode() != GameMode.CREATIVE){
			if(en.getType() == EntityType.ARMOR_STAND || en.getType() == EntityType.ITEM_FRAME || en.getType() == EntityType.MINECART || en.getType() == EntityType.MINECART_CHEST || en.getType() == EntityType.BOAT){
				e.setCancelled(true);
			}
		}
	}
	/*--------------------------------------------GESTION GRENADE----------------------------------------------*/
	@EventHandler
	public void onProjectileThrownEvent(PlayerEggThrowEvent e) {

		Egg egg = e.getEgg();

		egg.getWorld().createExplosion(egg.getLocation(), 0);
		egg.getWorld().playEffect(egg.getLocation(), Effect.MOBSPAWNER_FLAMES, 1);
		double radius = 5D;

		List<Entity> near = egg.getLocation().getWorld().getEntities();

		for(Entity en : near) {
			if(en instanceof Player){
				if(en.getLocation().distance(egg.getLocation()) <= radius){
					if(!(en.getLocation().getY() > 95.0)){
						((Player) en).damage(8.0);
						if(en.isDead()){
							Player t = (Player)en;
							Player k = (Player)egg.getShooter();

							if(en != k){
								t.sendMessage(Main.name+"Tu t'es fait exploser par: " + k.getName());
							}
						}
					}
				}
			}

		}
	}

}









